package design.graduation.novelsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import design.graduation.novelsystem.constant.Constant;
import design.graduation.novelsystem.mapper.MenuMapper;
import design.graduation.novelsystem.mapper.RoleMapper;
import design.graduation.novelsystem.model.domian.Menu;
import design.graduation.novelsystem.model.dto.ActiveAdmin;
import design.graduation.novelsystem.model.dto.DataGridView;
import design.graduation.novelsystem.model.dto.MenuTreeNode;
import design.graduation.novelsystem.model.vo.MenuVo;
import design.graduation.novelsystem.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 洪少尉
 * @Date: 2020/4/14 02:53
 * @Description: 
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService{
	@Autowired
	private RoleMapper roleMapper;

	/**
	 * //根据管理员ID去查询权限编码的集合
	 * @param adminId 管理员ID
	 * @return 权限编码的集合
	 */
	@Override
	public List<String> queryPermissionCodesByAdminId(Integer adminId) {
		//1.根据admin查询角色id的集合
		List<Integer> roleIds = this.roleMapper.queryRoleIdsByAdminId(adminId);
		//当集合不为空或长度大0
		if (null != roleIds && roleIds.size() > 0) {
			//2.根据角色id的集合，查询菜单的id的集合
			List<Integer> menuIds = this.roleMapper.queryMenuIdsByRids(roleIds);
			if (null != menuIds && menuIds.size() > 0) {
				QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
				//可用的菜单
				queryWrapper.eq(Menu.COL_AVAILABLE, Constant.AVAILABLE_TRUE)
						//菜单类型为权限类型
						.eq(Menu.COL_TYPE, Constant.MENU_TYPE_PERMISSION)
						//从获取的id集合中查找
						.in(Menu.COL_ID, menuIds)
						//结果以排序码升序
						.orderByAsc(Menu.COL_SORT_CODE);
				List<Menu> menus = this.baseMapper.selectList(queryWrapper);
				List<String> permissions = new ArrayList<>();
				//遍历取出的菜单集合，获取权限编码
				for (Menu menu : menus) {
					permissions.add(menu.getTypeCode());
				}
				//返回权限编码的集合
				return permissions;
			}
		}
		return null;
	}

	/**
	 * //根据当前管理员权限查询可用的登录目录
	 * @return 可用的登录目录
	 */
	@Override
	public Object getIndexMenu() {
		//等到当前登录的用户
		ActiveAdmin activeAdmin =(ActiveAdmin) SecurityUtils.getSubject().getPrincipal();
		//如果为超级管理员授权全部菜单
		List<Menu> menus = null;
		if(activeAdmin.getAdmin().getType().equals(Constant.USER_TYPE_SUPER)){
			menus = this.queryAllMenu();
		}else {
			menus = this.queryAllMenu(activeAdmin.getAdmin().getId());
		}
		List<MenuTreeNode> treeNodes=new ArrayList<>();
		for (Menu m : menus) {
			treeNodes.add(new MenuTreeNode(m.getId(),m.getPid(),m.getTitle(),m.getHref(),m.getIcon(),m.getTarget(),m.getTypeCode()));
		}
		List<MenuTreeNode> nodes = MenuTreeNode.MenuTreeNodeBuilder.build(treeNodes, 0);
		Map<String,Object> indexMenuList=new LinkedHashMap<>();
		Map<String,Object> map1=new LinkedHashMap<>();
		Map<String,Object> map2=new LinkedHashMap<>();
		map1.put("title","工作台");
		map1.put("href","page/welcome-1.html?t=1");
		map2.put("title","红袖阁");
		map2.put("image","images/logo.png");
		map2.put("href","");
		indexMenuList.put("homeInfo",map1);
		indexMenuList.put("logoInfo",map2);
		indexMenuList.put("menuInfo",nodes);
		return indexMenuList;
	}

	/**
	 * 通过id查询普通管理员的菜单
	 *
	 * @param id 管理员id
	 * @return 菜单
	 */
	@Override
	public List<Menu> queryAllMenu(Integer id) {
		//1.根据admin查询角色id的集合
		List<Integer> roleIds = this.roleMapper.queryRoleIdsByAdminId(id);
		//当集合不为空或长度大0
		if (null != roleIds && roleIds.size() > 0) {
			//2.根据角色id的集合，查询菜单的id的集合
			List<Integer> menuIds = this.roleMapper.queryMenuIdsByRids(roleIds);
			if (null != menuIds && menuIds.size() > 0) {
				QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
				//可用的菜单
				queryWrapper.eq(Menu.COL_AVAILABLE, Constant.AVAILABLE_TRUE)
						.eq(Menu.COL_TYPE,Constant.MENU_TYPE_TOP)
						.or()
						.eq(Menu.COL_TYPE,Constant.MENU_TYPE_LEFT)
						//从获取的id集合中查找
						.in(Menu.COL_ID, menuIds)
						//结果以排序码升序
						.orderByAsc(Menu.COL_SORT_CODE);
				List<Menu> menus = this.baseMapper.selectList(queryWrapper);
				return menus;
			}
		}
		return new ArrayList<>();
	}

	/**
	 * 返回超级管理员菜单
	 * @return 超级管理员菜单
	 */
	@Override
	public List<Menu> queryAllMenu() {
		QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(Menu.COL_TYPE,Constant.MENU_TYPE_TOP)
				.or()
				.eq(Menu.COL_TYPE,Constant.MENU_TYPE_LEFT)
				.orderByAsc(Menu.COL_SORT_CODE);
		if (this.baseMapper.selectList(queryWrapper) != null){
			return this.baseMapper.selectList(queryWrapper);
		}
		return new ArrayList<>();
	}

	/**
	 * 菜单表单数据
	 *
	 * @param menuVo 查询条件
	 * @return Menu数据
	 */
	@Override
	public DataGridView queryAllMenuAndPermission(MenuVo menuVo) {
		QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(menuVo.getAvailable() != null,Menu.COL_AVAILABLE,menuVo.getAvailable())
				.orderByAsc(Menu.COL_SORT_CODE);
		List<Menu> menus = this.baseMapper.selectList(queryWrapper);
		return new DataGridView((long) menus.size(),menus);
	}

	/**
	 * 保存菜单
	 * @param menu 菜单信息
	 * @return true/false
	 */
	@Override
	public boolean saveMenu(Menu menu) {
		if(menu.getType().equals(Constant.MENU_TYPE_TOP)){
			menu.setPid(0);
		}
		menu.setAvailable(Constant.AVAILABLE_TRUE);
		return 	this.save(menu);
	}

	/**
	 * 查询排序码的最大值
	 *
	 * @return 当前数据库表中排序码的最大值
	 */
	@Override
	public Integer queryMenuMaxSortCode() {
		return this.baseMapper.queryMenuMaxSortCode();
	}

	/**
	 * 删除时用于查询，当前菜单是否还有子菜单或权限
	 *
	 * @param id 菜单id
	 * @return 子菜单或权限个数
	 */
	@Override
	public Integer queryMenuChildrenCountById(Integer id) {
		QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(Menu.COL_PID,id);
		return this.baseMapper.selectCount(queryWrapper);
	}
}
